﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Kevin.Controls
{
    /// <summary>
    /// 校验器基类
    /// </summary>
    public abstract partial class KevinValidatorBase : Component
    {
        #region 构造函数

        public KevinValidatorBase()
        {
            //InitializeComponent();
        }

        public KevinValidatorBase(IContainer container)
        {
            container.Add(this);

            //InitializeComponent();
        }

        #endregion

        #region 属性

        private string _errorMessage = "";
        [Category("Properties")]
        [Description("错误提示信息")]
        [DefaultValue("")]
        public string ErrorMessage
        {
            get;
            set;
        }
        private Control _controlToValidat;
        [Category("Properties")]
        [Description("需要校验的控件")]
        public Control ControlToValidat
        {
            get { return _controlToValidat; }
            set
            {
                _controlToValidat = value;
                if (_controlToValidat != null && !DesignMode)
                {
                    _controlToValidat.Validating += new CancelEventHandler(ControlToValidate_Validating);
                }
            }
        }
        [Category("Properties")]
        [Description("校验错误提示图标")]
        public Icon ErrorIcon
        {
            get;
            set;
        }

        [Category("Properties")]
        [Description("校验成功提示图标")]
        public Icon SuccedIcon
        {
            get;
            set;
        }
        /// <summary>
        /// 是否校验
        /// </summary>
        [Browsable(false)]
        protected bool IsValid
        {
            get;
            set;
        }

        #endregion

        #region 方法

        protected abstract bool ValidateProcess();

        private void Validate()
        {
            IsValid = this.ValidateProcess();
            if (!IsValid)
            {
                ErrorProvider _errorProvider = new ErrorProvider();
                _errorProvider.Icon = this.ErrorIcon;
                _errorProvider.SetError(this.ControlToValidat, ErrorMessage);
            }
        }

        public void ControlToValidate_Validating(object sender, CancelEventArgs e)
        {
            Validate();
        }

        #endregion

    }
}
